In [22]:
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
diabetes = datasets.load_diabetes()
diabetes.data.shape
Out[22]:
In [23]:
# Use only one feature
diabetes_X = diabetes.data[:, np.newaxis, 2]
## Divide the dataset into train and test set
diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]
diabetes_y_train = diabetes.target[:-20]
diabetes_y_test = diabetes.target[-20:]
In [24]:
from sklearn import linear_model
regr = linear_model.LinearRegression()
regr.fit(diabetes_X_train, diabetes_y_train)
Out[24]:
In [25]:
# Get parameters
regr.get_params()
Out[25]:
In [26]:
print(regr.coef_)
In [27]:
# the mean square error
np.mean((regr.predict(diabetes_X_test)-diabetes_y_test) ** 2)
Out[27]:
In [28]:
# variance score: 1 is perfect prediction, 0 means that there is no linear relationship between X and y
regr.score(diabetes_X_test, diabetes_y_test)
Out[28]:
In [29]:
# Plot output
plt.scatter(diabetes_X_test, diabetes_y_test, color='black')
plt.plot(diabetes_X_test, regr.predict(diabetes_X_test), color = 'blue', linewidth=3)
plt.xticks(())
plt.yticks(())
plt.show()
In [ ]:
In [ ]: